package com.redlight.base;

/**
 * Created by chao on 2019/1/9.
 */
import com.google.common.collect.Maps;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.MapUtils;
import com.redlight.utils.StringUtil;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MapUtil extends MapUtils {
    public MapUtil() {
    }

    public static <T, V> T toObject(Map<String, V> map, Class<T> clazz) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        return toObject(map, clazz, false);
    }

    public static <T, V> T toObject(Map<String, V> map, Class<T> clazz, boolean toCamelCase) throws InstantiationException, IllegalAccessException, InvocationTargetException {
        if(toCamelCase) {
            map = toCamelCaseMap(map);
        }

        T object = clazz.newInstance();
        BeanUtils.populate(object, map);
        return object;
    }

    public static Map<String, String> toMap(Object object) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        return BeanUtils.describe(object);
    }

    public static <V> Map<String, V> toCamelCaseMap(Map<String, V> map) {
        Map<String, V> newMap = new HashMap();

        String key;
        String field;
        for(Iterator var2 = map.keySet().iterator(); var2.hasNext(); newMap.put(field, map.get(key))) {
            key = (String)var2.next();
            field = StringUtil.toCamelCase(key);
            if(StringUtil.isBlank(field)) {
                field = "";
            }
        }

        return newMap;
    }

    public static byte[] toByteArr(Map map) {
        byte[] bt = null;
        if(map != null) {
            ByteArrayOutputStream os = null;
            ObjectOutputStream oos = null;

            try {
                os = new ByteArrayOutputStream();
                oos = new ObjectOutputStream(os);
                oos.writeObject(map);
                bt = os.toByteArray();
            } catch (IOException var17) {
                var17.printStackTrace();
            } finally {
                try {
                    if(null != oos) {
                        oos.close();
                    }
                } catch (IOException var16) {
                    var16.printStackTrace();
                }

                try {
                    if(null != os) {
                        os.close();
                    }
                } catch (IOException var15) {
                    var15.printStackTrace();
                }

            }
        }

        return bt;
    }

    public static String[] getArray(Map map, Object key) {
        if(map != null) {
            Object answer = map.get(key);
            if(answer != null) {
                if(answer instanceof Object[]) {
                    return (String[])((String[])answer);
                }

                String[] retArr = new String[]{answer.toString()};
                return retArr;
            }
        }

        return new String[0];
    }

    public static long getLongIgnoreCaseKey(Map<String, Object> map, String getKey) {
        if(map == null) {
            return 0L;
        } else {
            Iterator var2 = map.entrySet().iterator();

            Map.Entry ent;
            Object val;
            do {
                if(!var2.hasNext()) {
                    return 0L;
                }

                ent = (Map.Entry)var2.next();
                val = ent.getValue();
            } while(!((String)ent.getKey()).equalsIgnoreCase(getKey));

            return null != val?Long.valueOf(val.toString()).longValue():0L;
        }
    }

    public static String getStringIgnoreCaseKey(Map<String, Object> map, String getKey) {
        if(map == null) {
            return "";
        } else {
            Iterator var2 = map.entrySet().iterator();

            Map.Entry ent;
            Object val;
            do {
                if(!var2.hasNext()) {
                    return "";
                }

                ent = (Map.Entry)var2.next();
                val = ent.getValue();
            } while(!((String)ent.getKey()).equalsIgnoreCase(getKey));

            return null != val?val.toString():"";
        }
    }

    public static Map newJsonMap(boolean success, int errorCode, String msg, Object data) {
        Map m = Maps.newHashMap();
        m.put("success", Boolean.valueOf(success));
        m.put("errorCode", Integer.valueOf(errorCode));
        m.put("message", msg);
        if(null != data) {
            if(data instanceof Map) {
                m.putAll((Map)data);
            } else {
                m.put("data", data);
            }
        }

        return m;
    }
}
